'use strict';

import React, { Component } from 'react';
import Base64Binary from '../../function/base64-binary' //base64
import ServerConfig from '../config/serverConfig'

import {
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  NativeModules,
} from 'react-native';

//按钮
class CustomButton extends React.Component {
  render() {
    return (
      <TouchableHighlight
        style={styles.button}
        underlayColor="#a5a5a5"
        onPress={this.props.onPress}>
        <Text style={styles.buttonText}>{this.props.text}</Text>
      </TouchableHighlight>
    );
  }
}


class m0UpadataScreen extends Component {
	_onpressInit=()=>{
	    NativeModules.BluetoothAndroid.enable();//打开蓝牙，然后需要手工配对，没有做搜索
	    NativeModules.BluetoothAndroid.getBondedDevices();//获取已经配对的设备
	    NativeModules.BluetoothAndroid.socketconnet();//用匹配到的蓝牙设备作为serve端进行socket连接
	    //console.log(NativeModules);
	};

  	_onpressSend=()=>{
  		// console.log(ServerConfig);
	   //  //fetch('http://192.168.1.107:7777/appdown/SYS.base64')
	   //  fetch(ServerConfig.APP_DOWN)
	   //    .then(res=>res.text())
	   //    .then((data)=>{
	   //    	var bin;
	   //      console.log(Base64Binary);
	   //      bin=Base64Binary.decode(data);
	   //      //console.log(data);
	   //      console.log(bin);
	   //    });

	   //  fetch(ServerConfig.getAllBinList)
	   //  	.then(res=>res.json())
	   //  	.then((json)=>{
	   //  		console.log(json[1]);
	   //  	});

	    var bytes=[0x11,0x22,0x33,0x44,0x55,0x66,0x77];
	    console.log(bytes);
	    NativeModules.BluetoothAndroid.socketwrite(bytes);//发送一个数据
	};

	_onpressClose=()=>{
	    NativeModules.BluetoothAndroid.socketclose();//发送一个数据
	};
	
	render() {
	    return (
	      <View>
	        <Text style={styles.welcome}>
	          蓝牙测试
	        </Text>
	        <CustomButton
	          text="蓝牙初始化"
	          onPress={this._onpressInit}
	        />
	        <CustomButton
	          text="发送一个数据"
	          onPress={this._onpressSend}
	        />
	        <CustomButton
	          text="关闭socket"
	          onPress={this._onpressClose}
	        />
	      </View>
	    );
	}
}

const styles = StyleSheet.create({
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
   button: {
    margin:5,
    backgroundColor: 'white',
    padding: 15,
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderBottomColor: '#cdcdcd',
  },
});


export default m0UpadataScreen;